//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.purvar.boot.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.purvar.boot.api.entity.SysAutocodeDetail;
import com.purvar.boot.api.mapper.MySQLGeneratorMapper;
import com.purvar.boot.api.mapper.SysAutocodeDetailMapper;
import com.purvar.boot.api.pojo.SysAutocodeDetailEditVo;
import com.purvar.boot.api.pojo.SysAutocodeDetailNewVo;
import com.purvar.boot.api.pojo.SysAutocodeDetailQueryVo;
import com.purvar.boot.api.service.ISysAutocodeDetailService;
import com.purvar.boot.auto.vo.TableVo;
import com.purvar.boot.comm.api.vo.BaseEntity;
import com.purvar.boot.comm.api.vo.Result;
import com.purvar.boot.comm.core.query.QueryGenerator;
import com.purvar.boot.comm.exception.BusinessException;
import com.purvar.boot.comm.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class SysAutocodeDetailServiceImpl extends ServiceImpl<SysAutocodeDetailMapper, SysAutocodeDetail> implements ISysAutocodeDetailService {
    private static final Logger log = LoggerFactory.getLogger(SysAutocodeDetailServiceImpl.class);
    @Autowired
    private MySQLGeneratorMapper mySQLGeneratorMapper;

    public SysAutocodeDetailServiceImpl() {
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    public void addSysAutocodeDetail(SysAutocodeDetailNewVo sysAutocodeDetailNewVo) {
        SysAutocodeDetail sysAutocodeDetail = new SysAutocodeDetail();
        BeanUtils.copyProperties(sysAutocodeDetailNewVo, sysAutocodeDetail);
        LambdaQueryWrapper<SysAutocodeDetail> query = new LambdaQueryWrapper();
        query.eq(BaseEntity::getDelFlag, "1");
        SysAutocodeDetail oneSysAutocodeDetail = (SysAutocodeDetail) this.getOne(query);
        if (null == oneSysAutocodeDetail) {
            sysAutocodeDetail.setDelFlag("1");
            ((SysAutocodeDetailMapper) this.baseMapper).insert(sysAutocodeDetail);
        } else {
            throw new BusinessException("", new Object[0]);
        }
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    public void updateSysAutocodeDetail(String id, SysAutocodeDetailEditVo sysAutocodeDetailEditDto) {
        SysAutocodeDetail sysAutocodeDetail = new SysAutocodeDetail();
        sysAutocodeDetail.setId(id);
        BeanUtils.copyProperties(sysAutocodeDetailEditDto, sysAutocodeDetail);
        ((SysAutocodeDetailMapper) this.baseMapper).updateById(sysAutocodeDetail);
    }

    public Result<IPage<SysAutocodeDetail>> listPage(SysAutocodeDetailQueryVo sysAutocodeDetailQueryVo, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        Result<IPage<SysAutocodeDetail>> result = new Result();
        SysAutocodeDetail sysAutocodeDetail = new SysAutocodeDetail();
        BeanUtils.copyProperties(sysAutocodeDetailQueryVo, sysAutocodeDetail);
        QueryWrapper<SysAutocodeDetail> queryWrapper = QueryGenerator.initQueryWrapper(sysAutocodeDetail, req.getParameterMap());
        Page<SysAutocodeDetail> page = new Page((long) pageNo, (long) pageSize);
        IPage<SysAutocodeDetail> pageList = this.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    public List<TableVo> getTableVos() {
        List<Map<String, Object>> maps = this.mySQLGeneratorMapper.queryList("");
        List<TableVo> tableVos = new ArrayList();

        TableVo tablevo;
        for (Iterator var3 = maps.iterator(); var3.hasNext(); tableVos.add(tablevo)) {
            Map<String, Object> map = (Map) var3.next();
            tablevo = new TableVo();
            tablevo.setTableDb(map.get("tablename").toString());
            if (map.get("tablecomment") != null && StringUtils.isNotEmpty(map.get("tablecomment").toString())) {
                tablevo.setTableName(map.get("tablecomment").toString());
            } else {
                tablevo.setTableName(map.get("tablename").toString());
            }
        }

        return tableVos;
    }
}
